﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_Role_View : System.Web.UI.Page
{
    public string sHtml = "", ll = "";
    public string sPCode = "";
    public string sPName = "";
    int nBtnCount = 0; //是否有被授权使用的按钮


    RoleManage MyRM = new RoleManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_User_LogonID"] == null)
        {
            Response.Redirect("../ReLogon.aspx");
        }
        if (!Page.IsPostBack)
        {
            ShowGrantTree();
        }
    }

    public void ShowGrantTree()
    {
        /* 控件树 */
        string sTreeHtml = "";
        string sRootLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/root_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRootSpreadIconHtml = "<img height=\"20\" src=\"img/sys/root_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sMidLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/mid_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sMidSpreadIconHtml = "<img height=\"20\" src=\"img/sys/mid_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRearLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/rear_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRearSpreadIconHtml = "<img height=\"20\" src=\"img/sys/rear_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sFolderCloseIconHtml = "<img height=\"20\" src=\"img/sys/folder_close.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sFolderOpenIconHtml = "<img height=\"20\" src=\"img/sys/folder_open.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sPageUnselectedIconHtml = "<img height=\"20\" src=\"img/sys/page_unselected.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sVerticalIconHtml = "<img height=\"20\" src=\"img/sys/vertical.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //string sButtonIconHtml = "<img height=\"20\" src=\"img/sys/button.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sBlankIconHtml = "<img src=\"img/sys/blank.gif\" align=\"absmiddle\" border=\"0\" WIDTH=\"19\" HEIGHT=\"20\">";

        string sUserRoleCode = Session["Baimasoft_Role_Code"].ToString().Trim();



        SqlDataReader ShowGrantTreeReader2 = MyRM.GetMyMenuTree(sUserRoleCode); 
        int nPageCount = 0; //是否有被授权使用的页面
        while (ShowGrantTreeReader2.Read())
        {
            //从数据库存储过程所创建的临时表中取数据
            string sIndentVertical = ShowGrantTreeReader2["IndentVertical"].ToString().Trim(); //展开结点树时，缩进的结点前面是否有上(几)级结点的垂线
            string isLastSibling = ShowGrantTreeReader2["LastSibling"].ToString(); //是否同族的最后一个结点
            string isLeafed = ShowGrantTreeReader2["Leafed"].ToString(); //是否有叶子结点
            string sCode = ShowGrantTreeReader2["Menu_Code"].ToString(); // 结点编码
            string cType = ShowGrantTreeReader2["Type"].ToString(); //结点类型
            int iHomologID = (int)ShowGrantTreeReader2["Page_ID"]; //若是页面其对应的页面ID
            string sLabel = ShowGrantTreeReader2["Label"].ToString(); //结点标签

            //组合html语句
            string sLabelHtml = "&nbsp;" + sLabel;
            if (sCode.Equals("Men"))
            { //根结点
                string sRootHtml = "";
                if (isLeafed.Equals("0"))
                { //无子结点
                    sRootHtml = sRootLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    break;
                }
                else
                    sRootHtml = sRootSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                sTreeHtml = sTreeHtml + "<div>" + sRootHtml + "</div>";
            }
            else
            { //非根结点
                string sNodeHtml = "";
                // 处理缩进 
                string sIndentHtml = "";
                int i = 0;
                char cIsVertical;
                while (i < sIndentVertical.Length)
                {
                    cIsVertical = sIndentVertical[i];
                    if (cIsVertical == '0')
                        sIndentHtml = sIndentHtml + sBlankIconHtml;
                    else
                        sIndentHtml = sIndentHtml + sVerticalIconHtml;
                    i = i + 1;
                }

                // 结点 
                if (cType.Equals("1"))
                { //1.树枝结点
                    if (isLastSibling.Equals("0"))
                    { //2.非尾结点
                        if (isLeafed.Equals("1")) //3.有子结点
                            sNodeHtml = sMidSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                        else //3.无子结点
                            sNodeHtml = sMidLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    }
                    else
                    { //2.尾结点
                        if (isLeafed.Equals("1")) //3.有子结点
                            sNodeHtml = sRearSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                        else //3.无子结点
                            sNodeHtml = sRearLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    }
                }
                else if (cType.Equals("0"))
                { //1.树叶结点
                    nPageCount++;
                    string BtnInfo = "";
                    BtnInfo = MyBtnInfo(sUserRoleCode, iHomologID);

                    string sPageChkboxHtml = "<input type=\"checkbox\" name=\"chkPage\"  disabled=\"disabled\"  value=\"" + iHomologID + "\" />&nbsp;&nbsp;" + BtnInfo;
                    if (isLastSibling.Equals("0")) //2.非尾结点
                        //sNodeHtml = sMidLeaflessIconHtml + sPageUnselectedIconHtml +sLabelHtml + sPageChkboxHtml + sBtnHtml;
                        sNodeHtml = sMidLeaflessIconHtml + sPageUnselectedIconHtml + sLabelHtml + sPageChkboxHtml;
                    else //2.尾结点
                        //sNodeHtml = sRearLeaflessIconHtml + sPageUnselectedIconHtml +sLabelHtml + sPageChkboxHtml + sBtnHtml;
                        sNodeHtml = sRearLeaflessIconHtml + sPageUnselectedIconHtml + sLabelHtml + sPageChkboxHtml;
                }
                sNodeHtml = sIndentHtml + sNodeHtml;
                sTreeHtml = sTreeHtml + "<div>" + sNodeHtml + "</div>";
            }
        }
        if (ShowGrantTreeReader2 != null)
            ShowGrantTreeReader2.Close();

        nBtnCount = MyRM.GetBtnCount();
        string sJS = "<script>form1.PCount.value=" + nPageCount + ";form1.BCount.value=" + nBtnCount + "</script>";
        sHtml = sTreeHtml + sJS;
    }

    //授权时的按钮信息
    public string MyBtnInfo(string MyOrgRole, int PageID)
    {
        //取该页面下的所有有权使用的有效的按钮结点...
        int nBtnCount = 0;
        string sMyBtnHtml = "&nbsp;&nbsp;&nbsp;";
        //返回临时表中的信息
        SqlDataReader ShowGrantTreeReader3 = MyRM.GetMyBtnInfo(PageID);
        while (ShowGrantTreeReader3.Read())
        {
            nBtnCount++;
            string sBtnLabel = ShowGrantTreeReader3["BtnName"].ToString();
            int iBtnID = (int)ShowGrantTreeReader3["Btn_ID"];
            string sBtnLabelHtml = "&nbsp;&nbsp;&nbsp;<font color=\"blue\">" + sBtnLabel + "</font>";
            string sBtnChkboxHtml = "<input type=\"checkbox\" name=\"chkBtn\" value=\"" + PageID + "." + iBtnID + "\" onclick=\"btnChkboxClick(this)\"/>";
            sMyBtnHtml = sMyBtnHtml + sBtnLabelHtml + sBtnChkboxHtml;
        }
        if (ShowGrantTreeReader3 != null)
            ShowGrantTreeReader3.Close();
        return sMyBtnHtml;
    }
}
